package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"example.com/hello/tracer"

	proto "example.com/hello/proto"
	micro "github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/client"
)

var logger = log.New(os.Stdout, "client", log.Ldate|log.Ltime|log.Llongfile)

func main() {
	// Create a new service
	service := micro.NewService(
		micro.Name("greeter.client"),
		micro.Tracer(tracer.MyTracer{}),
		micro.WrapClient(func(c client.Client) client.Client {
			log.Printf("client: %v, broker info: %v, register: %v", c,
				c.Options().Broker.Address(),
				c.Options().Registry)
			if ss, ok := c.Options().Registry.ListServices(); ok == nil {
				log.Println("services: ", ss)
			} else {
				log.Fatal("no service found")
			}
			return c
		}))
	// Initialise the client and parse command line flags
	service.Init()

	// Create new greeter client
	greeter := proto.NewGreeterService("greeter", service.Client())

	// Call the greeter
	rsp, err := greeter.Hello(context.TODO(), &proto.Request{Name: "John"})
	if err != nil {
		fmt.Println("err = ", err)
	}

	// Print response
	fmt.Println(rsp.Greeting)
}
